package com.td.erhome.ui.home;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.td.erhome.R;
import com.td.erhome.adapter.HouseMsgListAdapter;
import com.td.erhome.base.BaseFragment;
import com.td.erhome.models.ArticleBean;
import com.td.erhome.models.BaseBean;
import com.td.erhome.net.Api;
import com.td.erhome.net.BaseSubscriber;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class MsgFragment extends BaseFragment {

    @Bind(R.id.recyclview)
    XRecyclerView mRecyclerView;
    @Bind(R.id.tv_error)
    TextView mTvError;
    private int page = 1;
    private List<ArticleBean.ArticleListBean> mArticle_list = new ArrayList<>();
    private HouseMsgListAdapter mAdapter;
    private int mTotal_count;

    public static MsgFragment newInstance() {
        Bundle bundle = new Bundle();
        MsgFragment msgFragment = new MsgFragment();
        msgFragment.setArguments(bundle);
        return msgFragment;
    }

    @Override
    protected void init() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_msg;
    }

    @Override
    protected void initView(View view) {

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mAdapter.clearData();
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore() {
                if (mTotal_count > mAdapter.mArticle_list.size()) {
                    page++;
                    getData();
                }
            }
        });


        getData();
    }

    @Override
    protected void initOperation() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new HouseMsgListAdapter(getActivity(), mArticle_list);
        mRecyclerView.setAdapter(mAdapter);
    }

    public void getData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("page", page);
        Api.getRetrofit().getArticleList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseBean<ArticleBean>>(getActivity()) {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mRecyclerView.refreshComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mRecyclerView.refreshComplete();
                        mTvError.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onNext(BaseBean<ArticleBean> articleBeanBaseBean) {
                        super.onNext(articleBeanBaseBean);
                        mTotal_count = articleBeanBaseBean.getData().total_count;
                        mArticle_list = articleBeanBaseBean.getData().article_list;
                        mAdapter.addData(mArticle_list);
                        mRecyclerView.refreshComplete();

                        if (mAdapter.mArticle_list.size()==0){
                            mTvError.setVisibility(View.VISIBLE);
                        }else {
                            mTvError.setVisibility(View.GONE);
                        }

                    }
                });
    }


    @OnClick(R.id.tv_error)
    public void onViewClicked() {
        mAdapter.clearData();
        page =1;
        getData();
    }
}
